package com.xc.ezplan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.ezplan.admin.entity.AdminTencentConfig;
import com.xc.ezplan.admin.entity.AdminTencentContent;
import com.xc.ezplan.admin.entity.AdminTencentContent;
import com.xc.ezplan.admin.mapper.AdminTencentConfigMapper;
import com.xc.ezplan.admin.mapper.AdminTencentContentMapper;
import com.xc.ezplan.admin.service.AdminTencentContentService;
import com.xc.ezplan.admin.mapper.AdminTencentContentMapper;
import com.xc.ezplan.admin.vo.AdminTencentContentVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.SearchCondition;
import com.xc.ezplan.core.domain.TencentStorageConfig;
import com.xc.ezplan.core.utils.TencentCloudUtil;
import com.xc.ezplan.core.utils.FileUtil;
import com.xc.ezplan.core.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
@Primary
@RequiredArgsConstructor
public class AdminTencentContentServiceImpl extends ServiceImpl<AdminTencentContentMapper, AdminTencentContent>
    implements AdminTencentContentService{
    private final AdminTencentConfigMapper adminTencentConfigMapper;
    private final AdminTencentContentMapper adminTencentContentMapper;

    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadContent(MultipartFile file, Long configId) {
        AdminTencentConfig adminTencentConfig = adminTencentConfigMapper.selectById(configId);
        TencentStorageConfig aliStorageConfig = new TencentStorageConfig();
        BeanUtils.copyProperties(adminTencentConfig,aliStorageConfig);
        TencentCloudUtil.upload(aliStorageConfig,file.getOriginalFilename(),file.getBytes());
        AdminTencentContent adminTencentContent = new AdminTencentContent();
        adminTencentContent.setName(file.getOriginalFilename());
        adminTencentContent.setSize(FileUtil.getSize(Integer.parseInt(file.getSize()+"")));
        adminTencentContent.setConfigId(configId);
        adminTencentContent.setSuffix(FileUtil.getExtensionName(file.getOriginalFilename()));
        adminTencentContentMapper.insert(adminTencentContent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteContent(List<Long> ids, Long configId) {
        AdminTencentConfig adminTencentConfig = adminTencentConfigMapper.selectById(configId);
        TencentStorageConfig aliStorageConfig = new TencentStorageConfig();
        BeanUtils.copyProperties(adminTencentConfig,aliStorageConfig);
        List<String> fileNames = adminTencentContentMapper.selectList(new QueryWrapper<AdminTencentContent>().lambda().
                in(AdminTencentContent::getId, ids)).stream().map(content -> content.getName()).collect(Collectors.toList());
        TencentCloudUtil.delete(aliStorageConfig,fileNames);
        adminTencentContentMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContent(AdminTencentContentVo adminTencentContentVo) {

    }

    @Override
    public Object pageQueryContent(BaseSearchBody<AdminTencentContent> baseSearchBody) {
        Page<AdminTencentContent> page = baseSearchBody.getPage();
        SearchCondition searchCondition = baseSearchBody.getSearchCondition();
        List<Date> createTime = searchCondition.getCreateTime();
        LambdaQueryWrapper<AdminTencentContent> queryWrapper = new QueryWrapper<AdminTencentContent>().lambda().like(
                StringUtils.isNotBlank(searchCondition.getKeyWord()), AdminTencentContent::getName,
                searchCondition.getKeyWord()).eq(AdminTencentContent::getConfigId,searchCondition.getTencentConfigId());
        if (!CollectionUtils.isEmpty(createTime) && createTime.size() == 2) {
            queryWrapper.between(AdminTencentContent::getUpdateTime, createTime.get(0), createTime.get(1));
        }
        return adminTencentContentMapper.selectPage(page, queryWrapper);
    }
}




